package com.ganlinux.pangoo.controller.restful.base;

import com.alibaba.fastjson.JSON;
import com.ganlinux.pangoo.dao.model.User;
import com.ganlinux.pangoo.entity.request.ReqHeader;
import com.ganlinux.pangoo.entity.response.base.RspBase;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

import javax.servlet.http.HttpServletRequest;

/**
 * BlogController
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
@Before(BaseInterceptor.class)
public class BaseController extends Controller {
	public void index() {
		rederErroJson();
	}
	
	public void add() {
		rederErroJson();
	}
	
	public void save() {
		checkAccess();
	}
	
	public void edit() {
		rederErroJson();
	}
	
	public void update() {
		rederErroJson();
	}
	
	public void delete() {
		rederErroJson();
	}
	private void rederErroJson() {
		// TODO Auto-generated method stub
		RspBase rspBase = new RspBase();
		rspBase.setStatus(1);
		rspBase.setMessage("失败");
		renderJson(rspBase);
	}
	public boolean isCheckAccess() {
		// TODO Auto-generated method stub
		ReqHeader reqHeader = getHeaderFromRequest(getRequest());
		if(reqHeader != null){
			if (User.dao.checkAccess(reqHeader.getPin(),reqHeader.getSign())) {
				return true;
			}else{
				return false;
			}
		}
		/*String userid = getRequest().getHeader("userid");
		String tocken = getRequest().getHeader("Authorization");
		if (User.dao.checkAccess(userid,tocken)) {

		}else{
			RspBase rspBase = new RspBase();
			rspBase.setStatus(1);
			rspBase.setMessage("请重新登录");
			renderJson(rspBase);
		}*/
		return false;
	}

	private void checkAccess() {
		// TODO Auto-generated method stub
		ReqHeader reqHeader = getHeaderFromRequest(getRequest());
		if(reqHeader != null){
			if (User.dao.checkAccess(reqHeader.getPin(),reqHeader.getSign())) {

			}else{
				RspBase rspBase = new RspBase();
				rspBase.setStatus(1);
				rspBase.setMessage("身份验证失败");
				renderText(JSON.toJSONString(rspBase));
			}
		}
		/*String userid = getRequest().getHeader("userid");
		String tocken = getRequest().getHeader("Authorization");
		if (User.dao.checkAccess(userid,tocken)) {

		}else{
			RspBase rspBase = new RspBase();
			rspBase.setStatus(1);
			rspBase.setMessage("请重新登录");
			renderJson(rspBase);
		}*/
	}
	public ReqHeader getHeaderFromRequest(HttpServletRequest httpServletRequest){
		ReqHeader reqHeader = new ReqHeader();
		String client = getRequest().getHeader("client");
		reqHeader.setClient(client);
		String interfaceId = getRequest().getHeader("interfaceId");
		reqHeader.setInterfaceId(interfaceId);
		String clientVersion = getRequest().getHeader("clientVersion");
		reqHeader.setClientVersion(clientVersion);
		String buildNum = getRequest().getHeader("buildNum");
		reqHeader.setBuildNum(buildNum);
		String devBrand = getRequest().getHeader("devBrand");
		reqHeader.setDevBrand(devBrand);
		String devModel = getRequest().getHeader("devModel");
		reqHeader.setDevModel(devModel);
		String osVersion = getRequest().getHeader("osVersion");
		reqHeader.setOsVersion(osVersion);
		String screen = getRequest().getHeader("screen");
		reqHeader.setScreen(screen);
		String sign = getRequest().getHeader("sign");
		reqHeader.setSign(sign);
		String netType = getRequest().getHeader("netType");
		reqHeader.setNetType(netType);
		String pin = getRequest().getHeader("pin");
		reqHeader.setPin(pin);

		return reqHeader;
	}
}


